# ================================================#
#             Replication files for:              #
#   "The Legacy of Multi-Dimensional Conflict"    #
#         Joan Barceló and Leonid Peisakhin       #
#          Comparative Political Studies          #
#          Last updated: August 29, 2025          #
# ================================================#

# Replication files prepared using: "R version 4.2.3 (2023-03-15)"#

# ------------------#
# ------------------#
#    APPENDIX A.2   #
# ------------------#
# ------------------#

rm(list = ls())
library(dplyr)
library(ggplot2)
library(manifestoR)

PATH <- ''

mp_setapikey(paste0(PATH, "manifesto_apikey.txt"))
mpds <- mp_maindataset()

grab_party <- function(df, party_id, name, origin) {
  df %>%
    filter(countryname == "Spain", party == party_id) %>%
    transmute(
      Year      = as.numeric(substr(date, 1, 4)),
      PartyName = name,
      per301    = per301,
      per302    = per302,
      origin    = origin
    )
}

erc   <- grab_party(mpds, 33905, "ERC", origin = "catalan")
cup   <- grab_party(mpds, 33915, "CUP", origin = "catalan")
ciu   <- bind_rows(
  grab_party(mpds, 33611, "CIU/CDC/JxC", origin = "catalan"),  # CIU
  grab_party(mpds, 33099, "CIU/CDC/JxC", origin = "catalan"),  # CDC et al.
  grab_party(mpds, 33911, "CIU/CDC/JxC", origin = "catalan"),  # CDC
  grab_party(mpds, 33912, "CIU/CDC/JxC", origin = "catalan")   # JxC
)

pp    <- grab_party(mpds, 33610, "AP/PP", origin = "spanish")
psoe  <- grab_party(mpds, 33320, "PSOE", origin = "spanish")
cs    <- bind_rows(
  grab_party(mpds, 33440, "UPyD/Ciudadanos", origin = "spanish"),  # UPyD
  grab_party(mpds, 33420, "UPyD/Ciudadanos", origin = "spanish")   # C's
)
ucd   <- bind_rows(
  grab_party(mpds, 33430, "UCD/CDS", origin = "spanish"),  # UCD
  grab_party(mpds, 33512, "UCD/CDS", origin = "spanish")   # CDS
)
podem <- bind_rows(
  grab_party(mpds, 33220, "PCE/IU/Podemos/CSQEP/ICV", origin = "spanish"), # PCE/IU
  grab_party(mpds, 33210, "PCE/IU/Podemos/CSQEP/ICV", origin = "spanish"), # Podemos
  grab_party(mpds, 33096, "PCE/IU/Podemos/CSQEP/ICV", origin = "spanish")  # Podemos (alliance)
)
vox   <- grab_party(mpds, 33710, "Vox", origin = "spanish")

combined_df <- bind_rows(erc, ciu, cup, pp, cs, psoe, ucd, podem, vox) %>%
  filter(!is.na(Year), Year >= 1977, Year <= 2019) %>%
  arrange(PartyName, Year)

ymax <- max(c(abs(combined_df$per301), abs(combined_df$per302)), na.rm = TRUE)

FigureA2 <-
  ggplot() +
  geom_line(data = combined_df,
            aes(x = Year, y = per301, color = PartyName, group = PartyName)) +
  geom_point(data = combined_df,
             aes(x = Year, y = per301, color = PartyName, shape = PartyName),
             size = 2) +
  geom_line(data = combined_df,
            aes(x = Year, y = -per302, color = PartyName, group = PartyName)) +
  geom_point(data = combined_df,
             aes(x = Year, y = -per302, color = PartyName, shape = PartyName),
             size = 2) +
  theme_minimal() +
  scale_y_continuous(
    labels = abs,
    name   = "<--------- Centralization | Decentralization --------->",
    limits = c(-ymax, ymax)
  ) +
  scale_x_continuous(breaks = seq(1980, 2015, by = 5),
                     limits = c(1977, 2019), expand = c(0.02, 0.05)) +
  labs(x = "\nYear", y = "", title = "", color = "") +
  theme(
    legend.position = "bottom",
    panel.grid.minor = element_blank(),
    plot.margin = margin(0, 0, 0, 0, "pt")
  ) +
  guides(shape = "none")

print(FigureA2)
